/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package qrcodetool;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

/**
 *
 * @author zhanqxun
 */
public class PaintPanel extends JPanel {

    public static final int MAXHEIGHT = 400;
    BufferedImage image = null;
    String info = null;
    public ArrayList<Point> list = new ArrayList<Point>();

    public PaintPanel() {
        super();
        info = "";
        
    }

    public void setImage(BufferedImage image) {
        this.image = image;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    Point getOriPoint(int x,int y){
        if (image != null) {
            int w = image.getWidth();
            int h = image.getHeight();
            if (h > MAXHEIGHT) {
                double ratio = h / (double) MAXHEIGHT;
                return new Point((int)((x-30)*ratio),(int)((y-30)*ratio));
            }        
        }
        return null;
    }
    
    Point getPoint(int x,int y){
         if (image != null) {
            int w = image.getWidth();
            int h = image.getHeight();
            if (h > MAXHEIGHT) {
                double ratio = h / (double) MAXHEIGHT;
                return new Point((int)((x)/ratio),(int)((y)/ratio));
            }        
        }
        return null;
    }
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        if (image != null) {
            int w = image.getWidth();
            int h = image.getHeight();
            if (h > MAXHEIGHT) {
                double ratio = h / (double) MAXHEIGHT;
                w = (int) (w / ratio);
                h = MAXHEIGHT;
            }
          
            g.setColor(Color.red);
            g.drawRect(30 - 1, 30 - 1, w + 3, h + 3);
            g.drawImage(image, 30, 30,w,h, this);
            
            for(Point p : list){
                //System.out.println("draw circle at (" + p.x +"," + p.y +")");
                g.fillOval(30+p.x - 3, 30+p.y - 3, 6, 6);
            }
        }
        if (info != null && !info.equals("")) {
            if(info.length() > 120){
                g.drawString(info.substring(0, 60), 30, MAXHEIGHT + 60);
                g.drawString(info.substring(60,120), 30, MAXHEIGHT + 90);
                g.drawString(info.substring(120), 30, MAXHEIGHT + 120);
            }
            else if (info.length() > 60) {
                g.drawString(info.substring(0, 60), 30, MAXHEIGHT + 60);
                g.drawString(info.substring(60), 30, MAXHEIGHT + 90);
            } else {
                g.drawString(info, 30, MAXHEIGHT + 60);
            }
        }
    }

    void addPoint(Point point) {
        this.list.add(point);
    }
}
